package com.zhiyou100.design.mode.singleton;

/**
 * @author yang
 * @className SingletonDemo03
 * @packageName com.zhiyou100.design.mode.singleton
 * @Description TODO 单例模式第三版
 * @date 2020-08-13 21:27
 */
public class SingletonDemo03 implements AutoCloseable {
    /**
     * instance对象前面增加一个修饰符volatile。
     * <p>
     * 用最简单的方式理解, volatile修饰符阻止了变量访问前后的指令重排，保证了指令执行顺序。
     */
    private volatile static SingletonDemo03 instance = null;

    private SingletonDemo03() {
    }

    public static SingletonDemo03 getInstance() {
        if (instance == null) {
            synchronized (SingletonDemo03.class) {
                if (instance == null) {
                    instance = new SingletonDemo03();
                }
            }
        }
        return instance;
    }

    @Override
    public void close() throws Exception {

    }
}
